﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Service
{
    /// <summary>
    /// InvicationCodeUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class InvicationCodeUserControl : UserControl, IPlugin
    {

        private InvitationCodeService invitationCodeService = ServiceContainer.GetService<InvitationCodeService>();
        private SellerService sellerService = ServiceContainer.GetService<SellerService>();

        public InvicationCodeUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "邀请管理"; }
        }

        public Control UIControl
        {
            get { return this; }
        }


        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            this.pbBar.Parameters.Clear();
            int sellerId = 0;
            string code = this.tbCode.Text.Trim();
            DateTime startTime = this.dpStart.Value == null ? DateTime.MinValue : this.dpStart.Value.Value;
            DateTime endTime = this.dpEnd.Value == null ? DateTime.MinValue : this.dpEnd.Value.Value;
            int.TryParse(this.tbSellerId.Text.Trim(), out sellerId);


            this.pbBar.Parameters.Add("sellerId", sellerId);
            this.pbBar.Parameters.Add("code", code);
            this.pbBar.Parameters.Add("startTime", startTime);
            this.pbBar.Parameters.Add("endTime", endTime);

            this.pbBar.StartPage();
        }

        private void PageBarUserControl_PageChanging(object sender, PageBar.PageChangeEventArgs e)
        {
            var ret = this.invitationCodeService.GetByAll(e.GetParameter<int>("sellerId"), e.GetParameter<string>("code"), e.GetParameter<DateTime>("startTime"), e.GetParameter<DateTime>("endTime"), e.CurrentPage - 1, e.PageSize);
            this.pbBar.Total = ret.Total;
            this.pbBar.CurrentCount = ret.Datas.Count;
            var data = ret.Datas.Select(obj => new InvitationCodeViewModel(obj)).ToArray();
            foreach (var item in data)
            {
                item.Sellers = sellerService.GetAllByField("registerSource", item.Source.Code, 0, 0).Datas.ToArray();
            }
            this.lstItems.ItemsSource = data;
        }
    }
}
